package com.keste.model.fwk.util;

import com.keste.model.fwk.bean.EmailBean;

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;

public class SendEmailUtil {

    public SendEmailUtil() {
        super();
    }

    public static Boolean sendEmail(String from, String to, String bcc,
                                    String subject, String content) {
        try {
            // Get system properties
            Properties properties = System.getProperties();

            properties.setProperty("mail.smtp.host", "smtp.keste.com");
            properties.setProperty("mail.transport.protocol", "smtp");
            properties.setProperty("mail.smtp.auth", "false");

            // Get the default Session object.
            Session session = Session.getDefaultInstance(properties);

            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.setRecipients(Message.RecipientType.TO, to);

            // Set Bcc: header field of the header.
            if (bcc != null) {
                message.setRecipients(Message.RecipientType.BCC, bcc);
            }
            // Set Subject: header field
            message.setSubject("Keste Portal: " + subject);

            // Now set the actual message
            message.setText(content + "\n\nSent from Keste");

            // Send message
            Transport.send(message);
            System.out.println("Sent message successfully....");

        } catch (Exception mex) {
            mex.printStackTrace();
        }
        return true;
    }

    public static Boolean sendEmailNotification(EmailBean emailBean) {
        Boolean status =false;
        if(emailBean.getToEmail()!=null){
        status =
            sendEmail(emailBean.getFromEmail(), emailBean.getToEmail(),
                      emailBean.getBccEmail(), emailBean.getSubject(),
                      emailBean.getMessage());
        }
        return status;
    }

//    public static void main(String[] args) {
//        EmailBean emailBean = new EmailBean();
//        emailBean.setFromEmail("noreply@keste.com");
//        emailBean.setToEmail("priyanka.tadichetti@keste.com");
//        emailBean.setBccEmail("priyanka.tadichetti@keste.com");
//        emailBean.setSubject("Email Notification");
//        emailBean.setMessage("Test");
//        sendEmailNotification(emailBean);
//        //            sendEmail("abc@orc.com", "priyanka.tadichetti@keste.com", "ravi.puligilla@keste.com",
//        //                                                "Test", "For Test");
//
//    }

}
